package com.kkd.kkdapp.fragment;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.ManageAwardAdapter;
import com.kkd.kkdapp.http.entity.ManageAwardData;
import com.kkd.kkdapp.http.entity.ManageAwardDataList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class ManageAwardRegularFragment extends Fragment implements
		KKDScrollListener {

	private Activity mCurActivity;
	private View mContentView;

	private KkdXlistView mXListView;
	private int pageNO = 1;
	private int pageSize = 10;
	private String friendCustomerId;
	private List<ManageAwardDataList> dataList = null;
	private ManageAwardAdapter mAdapter;
	private boolean REFRESHFLAG = true;
	private ImageView img_null;
	private TextView tv_null;
	private Time_Date time_Date = new Time_Date();
	private String awrd;
	private ListView mListView;
	private int type;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			String time = "";

			switch (msg.what) {
			case 1:
				mXListView.stopLoadMore(-1);
				if (REFRESHFLAG) {
					mXListView.stopLoadMore(1);
				}
				break;
			case 2:
				mXListView.setNoMore(false);
				mAdapter.appendData(dataList, pageNO == 1);
				mAdapter.notifyDataSetChanged();
				mXListView.stopLoadMore(0);
				if (dataList.size() < pageSize) {
					mXListView.stopLoadMore(1);
				} else {
					mXListView.stopLoadMore(0);
				}
				time = time_Date.get_Year_Day();
				mXListView.setRefreshTime(time);
				break;
			case 3:
				mXListView.stopRefresh();
				time = time_Date.get_Year_Day();
				mXListView.setRefreshTime(time);
				break;
			case 4:
				AppUtil.makeToastForCustomer(mCurActivity, awrd);
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						mXListView.stopLoadMore(1);
						mXListView.stopRefresh();
					}
				}, 1000);
				if (REFRESHFLAG) {
					REFRESHFLAG = false;
					if (dataList == null) {
						img_null.setImageResource(R.drawable.img_null_net);
						tv_null.setText("网络错误，请稍候下拉刷新重试");
						img_null.setVisibility(View.VISIBLE);
						tv_null.setVisibility(View.VISIBLE);
					} else {
						if (dataList.size() != 0) {
							img_null.setVisibility(View.GONE);
							tv_null.setVisibility(View.GONE);
						} else {
							img_null.setImageResource(R.drawable.img_null_net);
							tv_null.setText("网络错误，请稍候下拉刷新重试");
							img_null.setVisibility(View.VISIBLE);
							tv_null.setVisibility(View.VISIBLE);
						}
					}
				}
				break;
			case 5:
				if (REFRESHFLAG) {
					REFRESHFLAG = false;
					if (dataList == null) {
						img_null.setImageResource(R.drawable.img_null);
						tv_null.setText("推荐好友理财，一起赚取奖励~");
						img_null.setVisibility(View.VISIBLE);
						tv_null.setVisibility(View.VISIBLE);
					} else {
						if (dataList.size() != 0) {
							img_null.setVisibility(View.GONE);
							tv_null.setVisibility(View.GONE);
						} else {
							img_null.setImageResource(R.drawable.img_null);
							tv_null.setText("推荐好友理财，一起赚取奖励~");
							img_null.setVisibility(View.VISIBLE);
							tv_null.setVisibility(View.VISIBLE);
						}
					}
				}
				break;
			default:
				break;
			}
		};
	};

	public static ManageAwardRegularFragment getInstance(int type) {
		ManageAwardRegularFragment manageAwardRegularFragment = new ManageAwardRegularFragment();
		Bundle bundle = new Bundle();
		bundle.putInt("type", type);
		manageAwardRegularFragment.setArguments(bundle);
		return manageAwardRegularFragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mCurActivity = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mContentView = inflater.inflate(R.layout.fragment_manage_award,
				container, false);
		type = getArguments().getInt("type");
		initView();
		getIntentMessage();
		return mContentView;
	}

	/**
	 * 初始化组件
	 */
	public void initView() {
		mXListView = (KkdXlistView) mContentView
				.findViewById(R.id.manageaward_lv);
		img_null = (ImageView) mContentView.findViewById(R.id.img_null);
		tv_null = (TextView) mContentView.findViewById(R.id.tv_null);
		mXListView.setKKDScrollListener(this);
		mListView = mXListView.mListView;
		mListView.setDividerHeight(0);
		mAdapter = new ManageAwardAdapter(mCurActivity);
		mListView.setAdapter(mAdapter);
		mXListView.startRefresh();
	}

	public void getIntentMessage() {
		Intent intent = mCurActivity.getIntent();
		friendCustomerId = intent.getStringExtra("customerId");
		questManageAward();
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 理财奖励
	 */
	public void questManageAward() {
		if (type == 1) {
			KkdHttpMagager.getInstance().getManageAwardManager(pageNO,
					pageSize, friendCustomerId, ManageAwardData.class,
					new HttpCallBack() {

						@Override
						public void onSuccess(int statusCode,
								Object responseBody) {
							ManageAwardData data = (ManageAwardData) responseBody;
							int pageCount = data.getPageCount();
							int pageNo = data.getPageNO();
							if (pageNo > pageCount) {
								handler.sendEmptyMessage(1);
							} else {
								dataList = data.getDataList();
								handler.sendEmptyMessage(2);
							}
							handler.sendEmptyMessage(3);
							handler.sendEmptyMessage(5);
						}

						@Override
						public void onFailure(int statusCode, String desc) {
							awrd = desc;
							handler.sendEmptyMessage(4);
							handler.sendEmptyMessage(5);
						}
					});
		} else if (type == 2) {
			KkdHttpMagager.getInstance().friendForFinancingPlan(
					friendCustomerId, pageNO, pageSize, ManageAwardData.class,
					new HttpCallBack() {

						@Override
						public void onSuccess(int statusCode,
								Object responseBody) {
							ManageAwardData data = (ManageAwardData) responseBody;
							int pageCount = data.getPageCount();
							int pageNo = data.getPageNO();
							if (pageNo > pageCount) {
								handler.sendEmptyMessage(1);
							} else {
								dataList = data.getDataList();
								handler.sendEmptyMessage(2);
							}
							handler.sendEmptyMessage(3);
							handler.sendEmptyMessage(5);
						}

						@Override
						public void onFailure(int statusCode, String desc) {
							awrd = desc;
							handler.sendEmptyMessage(4);
						}
					});
		}
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		if (AppUtil.isConnect(mCurActivity)) {
			if (!REFRESHFLAG) {
				REFRESHFLAG = true;
				questManageAward();
			}

		} else {
			mXListView.stopRefresh();
			AppUtil.makeToastForCustomer(mCurActivity, "似乎没有网络");
			if (dataList == null) {
				img_null.setImageResource(R.drawable.img_null_net);
				tv_null.setText("网络错误，请稍候下拉刷新重试");
				img_null.setVisibility(View.VISIBLE);
				tv_null.setVisibility(View.VISIBLE);
			} else {
				img_null.setVisibility(View.GONE);
				tv_null.setVisibility(View.GONE);
			}
			handler.postDelayed(new Runnable() {

				@Override
				public void run() {
					mXListView.stopLoadMore(0);
					mXListView.stopRefresh();
				}
			}, 1000);
		}
	}

	@Override
	public void onLoadMore() {
		if (AppUtil.isConnect(mCurActivity)) {
			pageNO++;
			questManageAward();
			REFRESHFLAG = false;
		} else {
			AppUtil.makeToastForCustomer(mCurActivity, "似乎没有网络");
			mXListView.stopLoadMore(0);
		}
	}

}
